package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;
import java.util.List;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.fragment.main.CourseFragment;
import cn.dujc.superkid.fragment.main.HomeFragment;
import cn.dujc.superkid.fragment.main.MatchFragment;
import cn.dujc.superkid.fragment.main.MeFragment;
import cn.dujc.superkid.fragment.main.TestFragment;
import cn.dujc.superkid.utils.Preferences;

/**
 * 由于适配平板，radiogroup图标不好调整，所以我改成linearlayout实现这个效果了
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    private final List<Integer> mTabs = Arrays.asList(R.id.home, R.id.course
            , R.id.match, R.id.test, R.id.me);
    private final Fragment[] mFragments = {new HomeFragment(), new CourseFragment()
            , new MatchFragment(), new TestFragment(), new MeFragment()};

    private View mLastSelected = null;

    @Override
    public int getViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initDefData();
        for (int id : mTabs) {
            findViewById(id).setOnClickListener(this);
        }
        initFragment(0);
    }

    public void refreshFragmentData() {


    }

    private void initDefData() {
        ChannelEntity channelEntity = Preferences.getInstance().getChannel(this);
        if (channelEntity == null) {
            ChannelEntity entity = new ChannelEntity();
            entity.setChannel_id("1");
            entity.setChannel_name("小学奥数");
            Preferences.getInstance().saveChannel(mActivity, entity);
        } else {
            String channelid = channelEntity.getChannel_id();
            if (TextUtils.isEmpty(channelid)) {
                ChannelEntity entity = new ChannelEntity();
                entity.setChannel_id(channelid);
                if (channelid.equals("1")) {
                    entity.setChannel_name("小学奥数");
                }else{
                    entity.setChannel_name("初中数学");
                }
                Preferences.getInstance().saveChannel(mActivity, entity);
            }
        }
    }

    long startTime = 0;

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if ((currentTime - startTime) >= 2000) {
            ToastUtil.showToast(this, "再按一次退出");
            startTime = currentTime;
        } else {
            finish();
        }
    }

    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        return null;
    }

    @Override
    public void onClick(View v) {
        initFragment(mTabs.indexOf(v.getId()));
    }

    public void initFragment(int index) {
        if (index < 0 || index >= mFragments.length) return;
        if (mLastSelected != null) mLastSelected.setSelected(false);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, mFragments[index])
                .commit();
        (mLastSelected = findViewById(mTabs.get(index))).setSelected(true);
    }
}
